﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using GoogleTranslationApiServices;
using log4net;

namespace LocalizationEditor
{
    public partial class GoogleApiKeyForm : Form
    {
        private ILog log = LogManager.GetLogger(typeof(GoogleApiKeyForm));

        public GoogleApiKeyForm()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the application configuration file.
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                GoogleTranslatorServiceSection section = config.GetSection("googleTranslatorServiceSection") as GoogleTranslatorServiceSection;

                if (section != null)
                {
                    section.ApplicationKey = textBoxKey.Text;
                    config.Save();
                    ConfigurationManager.RefreshSection("googleTranslatorServiceSection");
                }
            }
            catch (Exception x)
            {
                string message = "Unable to save to configuration file.";
                log.Error(message, x);
                MessageBox.Show(this, message);
            }   

            Close();
        }
    }
}
